package com.iwhalecloud.bss.kite.cucc.service.attr.handler;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.context.CuccServiceContext;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;

/**
 * @Classname LocalZxProvinceAttrHandler
 * @Description SDH AZ端省份属性
 * @Author
 * @Date 2021-03-05 18:10
 */
public class LocalZxProvinceAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private IUnicomRegionService unicomRegionService;

    @Override
    public void init(KiteAttr attr, KiteInst inst) {
        super.init(attr, inst);
        LocalAttrHelper.initPlatformProvince(attr, inst.getSpecId());
    }

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {

        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams)&& extParams.containsKey("nodeCode")) {
            return;
        }

        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(
            querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            || KiteStringUtils.equals(attr.getOldValue(), value)) {
            return;
        }


        //电路租用范围
        String otherValueField = KiteStringUtils.equals(attr.getFieldName(), "2_0000220")?"2_0000107":"2_0000220";
        String otherValue = inst.getAttrValue(otherValueField);
        String eRentScopeAttrValue = inst.getAttrValue("2_0000004");
        if (KiteStringUtils.isAnyEmpty(value,otherValue,eRentScopeAttrValue)){
            return ;
        }
    }

    /**
     * 获取页面展示名称
     *
     * @param attr 实例属性入参
     * @param inst 当前操作的实例对象
     * @param value 设置的值
     * @param data 属性处理入参，可以根据需要传入需要的业务数据
     * @return 属性页面展示名称
     */
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        StopWatch sw = new StopWatch("LocalZxProvinceAttrHandler");
        sw.start("LocalZxProvinceAttrHandler");
        List<UnicomRegion> unicomRegions = CuccServiceContext.getRegions(inst.getSpecId());
        if (KiteListUtils.isEmpty(unicomRegions)) {
            unicomRegions = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(inst.getSpecId()));
            CuccServiceContext.setRegions(inst.getSpecId(), unicomRegions);
        }
        sw.stop();
        System.out.println(sw.prettyPrint());
        for (UnicomRegion unicomRegion : unicomRegions) {
            if(value.equals(unicomRegion.getRegionCode())) {
                return unicomRegion.getRegionName();
            }
        }
        return "";
    }

    /**
     * 设置默认值，五大双线，Z端省份初始化不设置默认值，正常默认会取第一个枚举值为默认值，导致初始化是否跨域判断不对
     * @author zhang.song
     * @date 2022-02-18 10:05
     * @param attr
     * @param inst
     * @param data
     * @return void
     */
    @Override
    public void def(KiteAttr attr, KiteInst inst, AttrData data) {
        if(KiteStringUtils.isEqual(attr.getFieldName(), "2_0000107")) {
            String catagoryId = Optional.ofNullable(ProductCache.get(inst.getSpecId())).map(Product::getCateId).orElse(null);
            if(KiteStringUtils.equalsAny(catagoryId, ProdCateUtil.getDoubleLineCateIdForArr())) {
                return;
            }
        }

        super.def(attr, inst, data);
    }
}
